import { apiAddressAll, apiAreaAll } from 'js/api'
import { session } from 'js/util'

export default {
  state: {
    allAddress: [],
    allArea: session.get('allArea') || [],
    isLoading: false
  },
  getters: {
    allAddress: state => state.allAddress,
    allArea: state => state.allArea,
    isLoading: state => state.isLoading
  },
  actions: {
    // 获取所有地址列表
    fetchAllAddress({ state }, payload) {
      apiAddressAll(payload.token, 1, 20).then(data => {
        state.allAddress = data.addressList || []
        payload.callback && payload.callback(data.addressList || [])
      })
    },
    // 获取区域信息
    fetchAllArea({ state }, callback) {
      if (!state.allArea.length) {
        apiAreaAll().then(data => {
          state.allArea = data.areaList
          session.set('allArea', data.areaList)
          state.isLoading = true
          callback && callback()
        })
      } else {
        state.isLoading = true
        callback && callback()
      }
    }
  },
  mutations: {
  }
}
